package cn.nubia.admin.application.controller;

import cn.nubia.admin.application.service.FirewareService;
import cn.nubia.admin.application.bean.Channel;
import cn.nubia.admin.application.bean.ClientVersion;
import cn.nubia.admin.application.query.ChannelQuery;
import cn.nubia.admin.application.query.ClientQuery;
import cn.nubia.admin.application.service.ChannelService;
import cn.nubia.admin.application.service.ClientService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.sysconfig.bean.DictField;
import cn.nubia.admin.sysconfig.service.DictFieldItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.nubia.admin.application.bean.ClientVO;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by justin on 15/7/10.
 */
@Controller
@RequestMapping(value = "/admin/client/")
public class ClientController {

    @Autowired
    DictFieldItemService dictFieldItemService;
    @Autowired
    ClientService clientService;
    @Autowired
    FirewareService firewareService;
    @Autowired
    ChannelService channelService;

    /**
     * 展示新增APK的界面
     * @return
     */
    @RequiresPermissions(value = "soft:client:addApk")
    @RequestMapping(value = "add",method = RequestMethod.GET)
    public ModelAndView addApk(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("/client/addClientVersion");
        modelAndView.addObject("myReferer", request.getHeader("Referer"));

        addEnums(modelAndView);
        return modelAndView;
    }


    @ResponseBody
    @RequestMapping(value = "save")
    public Response<Boolean> save(@Valid ClientVO vo, BindingResult result){
        if(result.hasErrors())return Response.getFailedResponse(result.getAllErrors().get(0).getDefaultMessage());
        if(vo.getVersionCode()==null)return Response.getFailedResponse("客户端版本号不能为空");

        clientService.processSaveClient(vo);
        return Response.getSuccessResponse(true);
    }
    @ResponseBody
    @RequestMapping(value = "view")
    public ModelAndView view(Integer id){
        ModelAndView modelAndView = new ModelAndView("/client/editClientVersion");
        ClientVO vo = clientService.getClientVersion(id);
        modelAndView.addObject("vo",vo);
        addEnums(modelAndView);
        modelAndView.addObject("readonly", "readonly");
        return modelAndView;
    }
    @ResponseBody
    @RequestMapping(value = "edit")
    public ModelAndView edit(Integer id){
        ModelAndView modelAndView = new ModelAndView("/client/editClientVersion");
        ClientVO vo  = clientService.getClientVersion(id);
        modelAndView.addObject("vo", vo);
        addEnums(modelAndView);
        return modelAndView;
    }

    @RequiresPermissions(value = "soft:client:list")
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ModelAndView list(ClientQuery clientQuery){
        ModelAndView modelAndView = new ModelAndView("/client/clientList");
        Paged<ClientVersion>  clients = clientService.listClientVersions(clientQuery);
        Paged<Channel> channelPaged = channelService.listChannels(new ChannelQuery());
        modelAndView.addObject("channels",channelPaged.getListData());
        modelAndView.addObject("clients", clients);
        modelAndView.addObject("query",clientQuery);
        return modelAndView;
    }
    @ResponseBody
    @RequestMapping(value = "del",method = RequestMethod.GET)
    public boolean del(Integer id){
        return clientService.disableClientVersion(id);
    }

    @ResponseBody
    @RequestMapping(value = "upClient",method = RequestMethod.GET)
    public boolean upClient(Integer id){
        return clientService.changeState(id, ClientVersion.STATE_UP);
    }

    @ResponseBody
    @RequestMapping(value = "downClient",method = RequestMethod.GET)
    public boolean downClient(Integer id){
        return clientService.changeState(id, ClientVersion.STATE_SAVE);
    }

    private void addEnums(ModelAndView modelAndView){
        modelAndView.addObject("languages",dictFieldItemService.listItemsByDictName(DictField.DICTFIELDITEM_LANGUAGE));//语言
        modelAndView.addObject("firewares",firewareService.findAll());
        Paged<Channel> channelPaged = channelService.listChannels(new ChannelQuery());
        modelAndView.addObject("channels",channelPaged.getListData());
    }

}
